<?php
require_once(dirname(dirname(__FILE__)) . '/app.php');

need_login();

$request_uri = 'index';
$currentUrl = '/login.php';

$year = intval( $_GET['year'] );
$month = intval( $_GET['month'] );
$day = intval( $_GET['day'] );

$action = $_GET['action'];
if( $action == 'del' && $_GET['id'] > 0 ){
	if( $oauthMap[12] == null){
		exit;
	}
	Preengage::DeleteById($_GET['id'], $login_user_id);
}

if( $defaultShopId < 1 ){
	$defaultShopId = 1;
}

$shopList = Shop::GetAllShops();
$shop = Shop::getById($defaultShopId);


$serviceList = Service::GetAllByShopId($defaultShopId);


$tmp_date=date("YmdHi");
$tmp_year=substr($tmp_date,0,4) * 1;
$tmp_mon =substr($tmp_date,4,2) * 1;
$tmp_day = substr($tmp_date, 6, 2) * 1;
$tmp_Hour = substr($tmp_date, 8, 2) * 1;
$tmp_Minute = substr($tmp_date, 10, 2) * 1;

$searchDate = $_GET['search_date'];
$getDateArray = array();
if( preg_match("#/#", $searchDate) ){
	$getDateArray = explode("/", $searchDate);
}else if( preg_match("#-#", $searchDate) ){
	$getDateArray = explode("-", $searchDate);
}else{
	array_push($getDateArray, substr($searchDate, 0, 4));
	array_push($getDateArray, substr($searchDate, 4, 2));
	array_push($getDateArray, substr($searchDate, 6, 2));
}

$year = $getDateArray[0] * 1;
$month = $getDateArray[1] * 1;
$day = $getDateArray[2] * 1;

$year = $year > 0 ? $year : $tmp_year;
$month = $month > 0 ? $month : $tmp_mon;
$day = $day > 0 ? $day : $tmp_day;


$bookedList = array();
$serviceIdList = array();
foreach ($serviceList as $k => $v){
	$serviceIdList[$v['id']] = $v;
	$bookDayList = Preengage::GetDayListBySS($defaultShopId, $v['id'], $year, $month, $day);
	$bookedList[$v['id']] = $bookDayList;
}


$yearList = array();
for($tempYear = date('Y'), $index = 0; $index < 3; $index++){
	array_push($yearList, $tempYear - $index);
}
$monthList = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
$dayList = array();
for($index = 1; $index <= 31; $index++){
	array_push($dayList, $index);
}
$userAuth = OauthLimit::getOauthMap($login_user_id);

$hourList = array();
for($index = $shop['open_hour']; $index <= $shop['close_hour']; $index++){
	array_push($hourList, $index);
}
if( $userAuth[10] == null){
	exit;
}
$now = time();

include template('/service/service_index');


//include template('service_staff_index');
//include template('subscribe');
